package com.alpha.eceasy.core;

import java.io.Serial;
import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author huangpengfei
 */
public record R<T>(int code, String msg, T data) implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    public static <T> R<T> ok() {
        return restResult(null, 0, "ok");
    }

    public static <T> R<T>  ok(T data) {
        return restResult(data, 0, "ok");
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, 0, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, 400, "failed");
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, 400, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, 400, "failed");
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, 400, msg);
    }

    public static <T> R<T> failed(T data, Integer code, String msg) {
        return restResult(data, code, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        return new R<>(code, msg, data);
    }
}
